﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.FxCop.Sdk;

namespace LINQtoFxCop.Metrics
{
  public static class TypeMetrics
  {
    public static int NbMethods(this TypeNode type)
    {
      return type.Members.OfType<Method>().Count();
    }
    public static int NbFields(this TypeNode type)
    {
      return type.Members.OfType<Field>().Count();
    }
    public static int NbILInstructions(this TypeNode type)
    {
      return type.Members.OfType<Method>().Sum(method => method.NbILInstructions());
    }
    public static int NbInterfacesImplemented(this TypeNode type)
    {
      if (type == null)
        return 0;
      return InterfacesImplemented(type).Count();
    }
    private static IEnumerable<InterfaceNode> InterfacesImplemented(TypeNode type)
    {
      if (type.BaseType == null)
        return type.Interfaces;

      return type.Interfaces.Concat(InterfacesImplemented(type.BaseType)).Distinct();
    }
  }
}
